package ui.panel.warriors
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	import globals.ButtonType;
	import globals.Global;

	import manager.LinkManager;
	import manager.TooltipManager;

	import net.data.HeroInfo;

	import ui.component.GameTextField;
	import ui.component.Label;
	import ui.component.ScaleBitmap;
	import ui.component.TextButton;
	import ui.face.Bar;

	import util.DisplayUtil;
	import util.Long;
	import util.TextStyle;
	import util.UIUtil;

	/**
	 * 英雄列表的Item
	 * @author luguobin
	 */
	public class WarriorInfo extends Sprite
	{
		private var _heroId:Long;
		private var _bgItems:Vector.<ScaleBitmap>;
		private var _hero:Bitmap;
		private var _nameLabel:GameTextField;
		private var _jobLabel:GameTextField;
		private var _expBar:Bar; //经验
		private var _btnItems:Vector.<TextButton>;
		private var _isDispatch:Boolean;

		public function WarriorInfo()
		{
			super();
			initUI();
			this.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
		}

		private function onOverHandler(event:MouseEvent):void
		{
			DisplayUtil.removeChildAt(this, 0);
			DisplayUtil.addChildAt(_bgItems[1], this, 0);
		}

		private function onOutHandler(event:MouseEvent):void
		{
			DisplayUtil.removeChildAt(this, 0);
			DisplayUtil.addChildAt(_bgItems[0], this, 0);
		}

		private function initUI():void
		{
			_bgItems = new Vector.<ScaleBitmap>;
			var bg:ScaleBitmap = new ScaleBitmap(Global.assetsManager.getAssetsAsBitmapData("tower_bg"));
			bg.scale9Grid = new Rectangle(35, 35, 14, 14);
			bg.width = 150;
			bg.height = 335;
			this.addChild(bg);
			_bgItems.push(bg);
			bg = new ScaleBitmap(Global.assetsManager.getAssetsAsBitmapData("tower_blue_bg"));
			bg.scale9Grid = new Rectangle(35, 35, 14, 14);
			bg.width = 150;
			bg.height = 335;
			_bgItems.push(bg);

			_hero = new Bitmap();
			_hero.y = 15;
			this.addChild(_hero);

			_nameLabel = new GameTextField();
			_nameLabel.defaultTextFormat = TextStyle.getTextFormat(0xFFFFFF, 12, true, "center");
			_nameLabel.width = bg.width;
			_nameLabel.y = 115;
			this.addChild(_nameLabel);
			_jobLabel = new GameTextField();
			_jobLabel.defaultTextFormat = TextStyle.getTextFormat(0xFFFFFF, 12, false, "center");
			_jobLabel.width = bg.width;
			_jobLabel.y = 135;
			this.addChild(_jobLabel);

			_expBar = new Bar("experience_front", "experience_bg", true);
			_expBar.x = 16;
			_expBar.y = 160;
			this.addChild(_expBar);
			var btnTexts:Array = ["派驻", "装备", "训练", "晋级"];
			_btnItems = new Vector.<TextButton>;

			for(var i:int = 0; i < 4; i++)
			{
				var btn:TextButton = UIUtil.getTextButton(i, btnTexts[i], ButtonType.BUTTON_BLUE, btnHandle);
				btn.x = (bg.width - btn.width) * 0.5;
				btn.y = 190 + i * 30;
				this.addChild(btn);
				_btnItems.push(btn);
			}
		}

		private function btnHandle(button:TextButton):void
		{
			switch(button.id)
			{
				case 0:
					break;
				case 1: //装备
					LinkManager.getEquipByHero(_heroId);
					break;
				case 2:
					break;
				case 3: //晋级
					LinkManager.getHeroTraitUpInfo(_heroId);
					break;
			}
			Global.uiManager.warriorPanel.close();
		}

		public function update(heroInfo:HeroInfo):void
		{
			if(_isDispatch)
			{
				(_btnItems[0] as TextButton).enable = true;
				(_btnItems[1] as TextButton).enable = (_btnItems[2] as TextButton).enable = (_btnItems[3] as TextButton).enable = false;
			}
			else
			{
				(_btnItems[0] as TextButton).enable = false;
				(_btnItems[1] as TextButton).enable = (_btnItems[2] as TextButton).enable = (_btnItems[3] as TextButton).enable = true;
			}

			_heroId = heroInfo.heroId;
			_hero.bitmapData = Global.assetsManager.getAssetsAsBitmapData("avatar_1_1", "warrior");
			_hero.x = (this.width - _hero.width) / 2;
			TooltipManager.getInstance().addComponentTooltip(_hero, "1111111111111");
			_nameLabel.text = "英雄名字"; //名字，职业从heroCode模板里拿
			_jobLabel.text = "弓箭手 lv.2";
			_expBar.update(heroInfo.experience, heroInfo.experience + 100);
		}

		public function clear():void
		{
			_hero.bitmapData = null;
			_nameLabel.text = "";
			_jobLabel.text = "";
			_expBar.update(0, 100);
			(_btnItems[0] as TextButton).enable = (_btnItems[1] as TextButton).enable = (_btnItems[2] as TextButton).enable = (_btnItems[3] as TextButton).enable = false;
		}

		public function get isDispatch():Boolean
		{
			return _isDispatch;
		}

		public function set isDispatch(value:Boolean):void
		{
			_isDispatch = value;
		}

	}
}